package EGS.impl;
import java.util.concurrent.ExecutorService;

import org.dom4j.Document;
import org.dom4j.Element;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.stationClient;

public class EGS_execute_service10001Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 10001 登记条码刻码完成
	 */
	public EGS_execute_service10001Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if (msgClass.equals("10001")) {// 登记条码
				Element elePanel = Mroot.element("body").element("MPPCBTagReq");
				String panelSN = null;
				String panelResult = null;
				String iOrder=null;
				try {
					iOrder=elePanel.attribute("manufacturelotno").getValue();
					panelSN = elePanel.attribute("barcode").getValue();
					panelResult = elePanel.attribute("result").getValue();
				} catch (Exception e) {
					;
				}
				if (panelSN == null) {
					panelSN = "";
				}
				if (panelResult == null) {
					panelResult = "";
				}
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				try {
					iClient.orderActiveRequired = true;
//					iClient.getStationSetting(iStation);
					iClient.myLayer = 2;
					String gPanelSN = panelSN;
					boolean bState = false;
					if (panelResult.equals("0")) {
						bState = true;
					}
					bState = true;// 2020-12-18 由于有刻码机不传拼板结果，并且没有刻码机开启读码功能，修改为所有10001视为PASS
					if (bState) {
						String iOrderNumber=iOrder;
						new Thread(new Runnable() {
							String mPanelSN = gPanelSN;
							@Override
							public void run() {
								boolean flag = true;
								try {
									if(iOrderNumber!=null) {
										iClient.freshOrderWipQty_II(iOrderNumber);
									}
									flag = true;
									while (flag) {
										try {
											sql.Command("update order_serialNumber set releaseFlag=2 " + "where panelSN='"
													+ mPanelSN + "'");
											flag = false;
											if(iOrderNumber!=null) {
												iClient.freshOrderWipQty_II(iOrderNumber);
											}
										} catch (Exception e) {
											flag = true;// 如果sql连接失败，重试
											Thread.yield();
											try {
												Thread.sleep(300);
											} catch (InterruptedException e1) {
												;
											}
										}
									}
								} catch (Exception e2) {
									;
								}
								
							}
						}).start();
						iClient.uploadPanelState(panelSN, true,"-1");
					} else {
						new Thread(new Runnable() {
							String mPanelSN = gPanelSN;

							@Override
							public void run() {
								boolean flag = true;
								try {

									flag = true;
									while (flag) {
										try {
											sql.Command("update order_serialNumber set state=2 " + "where panelSN='"
													+ mPanelSN + "'");
											flag = false;
										} catch (Exception e) {
											flag = true;// 如果sql连接失败，重试
											Thread.yield();
											try {
												Thread.sleep(300);
											} catch (InterruptedException e1) {
												;
											}
										}
									}
								} catch (Exception e2) {
									;
								}
							}
						}).start();
					}
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100011, iStation + "_panel=" + panelSN);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100010, iStation + "_panel=" + panelSN + ":_" + str);
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

}
